﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PefStudio.Windows;

namespace PefStudio.Windows.Menus
{
    [Export(MainWindow.MainMenuContract, typeof(IMenuItem))]
    sealed class MainWindowMenu : AbstractMenuItem, IPartImportsSatisfiedNotification
    {
        public const string Contract = "PefStudio.Windows.MainMenu.WindowMenu";

        public MainWindowMenu()
        {
            this.ID = "Window";
            this.Text = "&Window";
            Index = 5.0;

            this.InsertRelativeToID = "Tools";
        }

        [ImportMany(Contract, typeof(IMenuItem), AllowRecomposition = true)]
        private IEnumerable<IMenuItem> MenuItems { get; set; }

        private int _preMenuItemCount = 0;
        public void OnImportsSatisfied()
        {
            CreateService.CreateMenu(this.DropDownItems, MenuItems, MainWindow);
            _preMenuItemCount = this.DropDownItems.Count;

            MainWindow.ActiveDocumentChanged += new EventHandler(MainWindow_ActiveDocumentChanged);
            MainWindow.TitleChanged += new EventHandler(MainWindow_TitleChanged);
        }

        void MainWindow_ActiveDocumentChanged(object sender, EventArgs e)
        {
            BulidSubDropDownItems();
        }

        void MainWindow_TitleChanged(object sender, EventArgs e)
        {
            BulidSubDropDownItems();
        }

        private readonly List<ToolStripItem> _subDropDownItems = new List<ToolStripItem>();
        private void BulidSubDropDownItems()
        {
            for (int i = this.DropDownItems.Count - 1; i >= _preMenuItemCount; i--)
            {
                if (_subDropDownItems.Contains(this.DropDownItems[i]) == true)
                    this.DropDownItems.RemoveAt(i);
            }
            _subDropDownItems.Clear();
            _subDropDownItems.Add(new ToolStripSeparator());

            int count = 1;
            foreach (AbstractDocument doc in MainWindow.Documents)
            {
                if (doc is AbstractPad)
                    continue;

                var item = new ToolStripMenuItem((count++).ToString() + " " + doc.Title) {Tag = doc};
                if (doc == MainWindow.ActiveDocument)
                    item.Checked = true;
                item.Click += new EventHandler(item_Click);

                _subDropDownItems.Add(item);

                if (doc is AbstractDockDocument)
                {
                    var dockDoc = doc as AbstractDockDocument;
                    int subCount = 1;
                    foreach (AbstractDocument subDoc in dockDoc.Documents)
                    {
                        var subItem = new ToolStripMenuItem((subCount++).ToString() + " " + subDoc.Title) {Tag = subDoc};
                        if (item.Checked == true && subDoc == dockDoc.ActiveDocument)
                            subItem.Checked = true;
                        subItem.Click += new EventHandler(subItem_Click);

                        item.DropDownItems.Add(subItem);
                    }
                }
            }
            this.DropDownItems.AddRange(_subDropDownItems.ToArray());
        }

        void item_Click(object sender, EventArgs e)
        {
            var item = sender as ToolStripMenuItem;
            if (item == null)
                return;

            var doc = item.Tag as AbstractDocument;
            if (doc == null)
                return;

            doc.Activate();
        }

        void subItem_Click(object sender, EventArgs e)
        {
            var item = sender as ToolStripMenuItem;
            if (item == null)
                return;

            var doc = item.Tag as AbstractDocument;
            if (doc == null)
                return;

            if (doc.ParentDockDocument != null)
                doc.ParentDockDocument.Activate();
            doc.Activate();
        }
    }

    [Export(MainWindowMenu.Contract, typeof(IMenuItem))]
    sealed class MainWindowMenuCloseAll : AbstractMenuItem
    {
        public MainWindowMenuCloseAll()
        {
            ID = "CloseAll";
            Text = "Close All";
        }

        public override void ConditionEvaluate()
        {
            base.ConditionEvaluate();

            Enabled = MainWindow.Documents.Count != 0;
        }

        protected override void OnClick(EventArgs e)
        {
            for (int i = MainWindow.Documents.Count - 1; i >= 0; i--)
            {
                MainWindow.Documents[i].Close();
            }
        }
    }
}
